package com.rentong.wx.filter;

import com.rentong.basic.BusinessException;
import com.rentong.constants.GlobalConstant;
import com.rentong.wx.cust.dto.WxUserInfo;
import com.rentong.wx.cust.service.WxCustUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WxLoginInterceptor extends HandlerInterceptorAdapter {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private WxCustUserService apiWxUserService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        if ("/sysConf/getConfInformation".equals(requestURI)) {
            return true;
        }
        if ("/back/wx/pay/notify".equals(requestURI)) {
            return true;
        }
        String token = request.getParameter("token");
        if (StringUtils.isBlank(token)) {
            logger.error("wx账号未登陆");
            throw new BusinessException("110", "未登录");
        }
        WxUserInfo wxUserInfo = apiWxUserService.getCustInfo(token);
        request.getSession().setAttribute(GlobalConstant.SESSION_WX_USER, wxUserInfo);
        return super.preHandle(request, response, handler);
    }


}
